﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using ContractorTimesheet.DataModel;

namespace ContractorTimesheet.Repository
{
    public class HolidayRepository : BaseRepository
    {
        TimesheetDBModel dataModel = new TimesheetDBModel();

        public IList<Holiday> GetAllHolidays()
        {
            return dataModel.Holidays.ToList();
        }

        public IList<Holiday> GetHolidays(int month)
        {
            return dataModel.Holidays.ToList().Where(m => int.Parse(m.Month) == month).ToList();
        }

        public void Add(Holiday holiday)
        {
            try
            {
                dataModel.Holidays.Add(holiday);

                dataModel.SaveChanges();

            }
            catch (DbEntityValidationException ex)
            {
                throw new Exception(GetErrorMessage(ex));
            }
        }

        public async void DeleteAsync(int id)
        {
            Holiday holiday = dataModel.Holidays.Single(o => o.Id == id);

            dataModel.Holidays.Remove(holiday);

            await dataModel.SaveChangesAsync();
        }
    }
}